package com.fun.feifun.modules.sys.user.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fun.feifun.modules.sys.auth.model.dto.MenuTree;
import com.fun.feifun.modules.sys.auth.service.ISysMenuService;
import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
import com.fun.feifun.modules.sys.user.service.ISysUserService;
import com.fun.feifun.modules.sys.user.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fun.feifun.base.result.Result;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author wkf
 * @since 2023-07-03
 */
@RestController
@Api(value = "/sys/user", tags = "系统-用户")
@Slf4j
@SaCheckLogin()
@RequestMapping("/sys/user")
    public class SysUserController {


    @Autowired
    private ISysUserService service;
    @Resource
    private ISysMenuService sysMenuService;

    /**
     * 获取分页U
     */
    @PostMapping("/page")
    @ApiOperation(value = "获取分页信息")
    public Result<Page<SysUser>> page(@RequestParam(name = "page", defaultValue = "1") int index,@RequestParam(name = "size", defaultValue = "20") int size){
        Page page = new Page(index,size);
        IPage page1 = service.page(page);
        return Result.success(page);
    }


    /**
     * 添加数据
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "添加或者保存")
    public Result save(@RequestBody SysUser item){
        BASE64Encoder encoder=new BASE64Encoder();
        String encodePassword = encoder.encode(item.getPassword().getBytes());
        item.setPassword(encodePassword);
        if(service.saveOrUpdate(item)){
         return Result.success("数据添加成功！");
        }
        return Result.err("数据添加成功！");
    }



    /**
     * 删除数据
     */
    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public Result deleteItems(@RequestParam("ids") List<Long> ids){
        if(service.removeByIds(ids)){
            return Result.success("数据删除成功！");
        }
        return Result.err("数据删除失败");
    }



    /**
     * 获取全部数据
     */
    @SaCheckLogin()
    @GetMapping("/all")
    @ApiOperation(value = "获取全部数据")
    public Result<List<SysUser>> getAll(){
        List<SysUser> all = service.list();
        return Result.success(all);
    }

    /**
     * 根据ID查找数据
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查找")
    public Result findById(@RequestParam("id") Long id){
        SysUser item = service.getById(id);
        if(item==null){
            return Result.err("尚未查询到此ID");
        }
        return Result.success(item);
    }

    @SaCheckLogin()
    @GetMapping("/getMenuTree")
    @ApiOperation(value = "获取用户菜单树")
    public Result getMenuTree(@RequestParam("id") Long id){
        if(id==null||id==0l){
            SaTokenInfo info = StpUtil.getTokenInfo();
            id=(Long)info.getLoginId();
        }
        List<MenuTree> treeByUserId = sysMenuService.getTreeByUserId(id);
        return Result.success(treeByUserId);
    }


}

